﻿

import Request from '../../base/request.js?t=202505281520'
import Storage from '../../base/storage.js?t=202505281520'
import { imageBaseUrl, getBrowserInfo } from "../../base/utils.js?t=202505281520"
const Composition = (proxy) => {
    const data = reactive({
        proxy: "",
        messageTest: "",
        formState: {
            dept: "",
            name: "",
            job: "",
            role: "",
            tel: "",
        },
        loading: true,
        pagination: {
            total: 0,
            current: 1,
            pageSize: 10,
            showTotal: total => `总共 ${total} 项`,
            defaultPageSize: 10,
            // pageSizeOptions: ['5', '10', '15', '20'], // 可不设置使用默认
            showSizeChanger: true, // 是否显示pagesize选择
            showQuickJumper: false, // 是否显示跳转窗
        },
        deptList: [
        ],
        roleList: [
        ],


        memberList: [],

        chooseMember: {},


        rules: {
            dept:[{ required: true, message: '请选择部门', trigger: 'change' }],
            name: [{ required: true, message: '请选择成员', trigger: 'change' }],

        },
        opType:"",
        confirmShow:false,
        delId: "",

        open: false,

        queryParams: {
            role: null,
            dept: null,
        },
        statusList: [
            { name: '组长', code: '0' },
            { name: '组员', code: '1' },

        ],
 
  
        columns: [
            {
                title: '序号',
                dataIndex: 'index',
                key: 'index',
                align: 'center',
                width: 80,
                customRender: ({ text, record, index }) => {
                    return (params.pagination.value.current - 1) * params.pagination.value.pageSize+  index + 1
                },
            },
            {
                title: '成员姓名',
                dataIndex: 'MemberName',
                key: 'MemberName',
                align: 'center'

            },
            {
                title: '所属部门',
                dataIndex: 'MemberDepartment',
                key: 'MemberDepartment',
                align: 'center'
            },
            {
                title: '成员职务',
                dataIndex: 'JobTitle',
                key: 'JobTitle',
                align: 'center'
            },
            {
                title: '成员角色',
                dataIndex: 'JobRoleName',
                key: 'JobRoleName',
                align: 'center'
            },
            {
                title: '联系电话',
                dataIndex: 'Phone',
                key: 'Phone',
                align: 'center'
            },
            {
                title: '更新时间',
                dataIndex: 'UpdateTime',
                key: 'UpdateTime',
                align: 'center',
                customRender: function ({ text, record, index, column }) {
                    const regex = /\/Date\((\d+)\)\//;
                    const match = text&& text.match(regex);
                    if (match) {
                        let res = dayjs(parseInt(match[1], 10)).format('YYYY-MM-DD HH:mm')
                        return res
                    } else {
                        return ""
                    }
              
                }
            },
            {
                title: '操作',
                key: 'action',
                align: 'center',
                customRender: function ({ text, record, index, column }) {
                    const fDeleteBtn = h(antd.Popconfirm, {
                        title: `提示：审核组成员【${record.MemberName}】，删除后将无法恢复，请您确认。`,
                        onConfirm: () => methods.handleconfirmOk(record.Id)

                    }, { default: () => h(antd.Button, {
                            style: 'margin-right:4px',
                            type: "link",
                            danger:true,
                            onClick: () => { methods.delData(record.Id) }
                        }, { default: () => '删除' }) })
                    return h('div', {
                    },
                        [h(antd.Button, {
                            style: 'margin-right:4px',
                            type: "link",
                            onClick: () => { methods.handleDialog(1, record.Id)}
                        }, { default: () => '编辑' }), h(antd.Button, {
                            style: 'margin-right:4px',
                            type: "link",
                            onClick: () => { methods.handleDialog(2, record.Id) }
                        }, { default: () => '详情' }), fDeleteBtn ]
                    );



                }
            },
        ],
        dataSource: [],
        sdeptList: [
            { name: '公司办公室', code: '0' },
            { name: '发展规划部', code: '1' },
            { name: '财务部', code: '2' },
            { name: '人力资源部', code: '3' },
            { name: '审计部', code: '4' },
            { name: '法律部', code: '5' },
            { name: '党委工作部', code: '6' },
            { name: '监督检查办公室', code: '7' },
            { name: '工会', code: '8' },
            { name: '团委', code: '9' },
            { name: '项目管理部', code: '10' },
            { name: '生产管理部', code: '11' },
            { name: '科技管理部', code: '12' },
            { name: '制造工程技术中心', code: '13' },
            { name: '采供物流中心', code: '14' },
            { name: '质量管控中心', code: '15' },
            { name: '计量测试中心', code: '16' },
        ],
    })
    let params = toRefs(data)

    const methods = {
        filterOption: (val, type) => {
            return `${type.value}`.includes(`${val || ''}`)
        },
        queryList: () => {
            //const { role, dept } = params.queryParams.value
            methods.getTableList(true)
            console.log(params.queryParams.value)
        },
        filterOption1: (val, type) => {

            return `${type.label}`.includes(`${val || ''}`)
        },
        filterOption2: (val, type) => {
            return `${type.label}`.includes(`${val || ''}`)
        },
        handleTableChange: (page) => {
            const { current, pageSize } = page
            params.pagination.value.current = current;
            params.pagination.value.pageSize = pageSize;
            methods.getTableList()
        },

        handleDialog: (type, id) => {

      
            params.opType.value = type

            if (type == 0) {
                //新增
                    params.proxy.value.$refs.formRef&&  params.proxy.value.$refs.formRef.resetFields();
            }
            else  {
                Request({
                    url: "/Performance/GetTeamMemberDetail",
                    method: 'post',
                    data: {
                        Id: id
                    }
                }).then(res => {
                    let data = res.Data
                    params.chooseMember.value = data
                    //params.chooseMember.value.JobRole = +data.JobRole
                    params.formState.value.dept = data.DepartmentId

   
                    Request({
                        url: "/SystemConfig/GetUserListByDepartment",
                        method: 'post',
                        data: {
                            DepartmentId: data.DepartmentId
                        }
                    }).then(res => {

  
                        params.memberList.value = res.Data.map(e => {
                            return {
                                label: `${e.UserName}-${e.UserNo}`,
                                value: `${e.Phone}-${e.UserNo}`,

                            }
                        })



                        params.formState.value.name = `${data.Phone}-${data.UserNo}`
                        params.formState.value.tel = data.Phone
                        params.formState.value.role = data.JobRole
                        params.formState.value.job = data.JobTitle
                    })

           
                })


                  //修改

            }
            //opType  0 新增  1 修改 2详情

            params.open.value = true
        },


        getTableList: (isRresh) => {
            if (isRresh) {
                params.pagination.value.current = 1
            }
            params.loading.value = true
            Request({
                url: "/Performance/GetTeamMemberPageList",
                method: 'post',
                params: {
                    page: params.pagination.value.current,
                    pageSize: params.pagination.value.pageSize
                },
                data: {
                    JobRole: params.queryParams.value.role,
                    DepartmentId: params.queryParams.value.dept
                }
            }).then(res => {
                params.dataSource.value = res.Data.Data
                params.pagination.value.total = res.Data.TotalItemCount
                params.loading.value = false
                //params.messageTest.value.success(res.Msg);
            })
        
        },


        init: () => {
              getBrowserInfo().catch(err => { console.log(err)})
            if (proxy) {
                params.proxy.value = proxy
            }
            const { message } = antd
            params.messageTest.value = message
            params.messageTest.value.config({
                top: `10vh`,
                duration: 7,
                maxCount:1,
                rtl: true,
            });
        
            let info = Storage.get('AccountInfo', true)
            Request({
                url: "/Account/createJwtToken",
                method: 'post',
                data: {
                    "userName": info && info.UserNo
                }
            }).then(res => {
                Storage.set('token', res.Data,true)
                methods.getTableList(true)
                   
                Request({
                    url: "/performance/GetJobRoleList",
                    method: 'post',
                }).then(res => {
                    console.log(res)
                    params.roleList.value = res.Data.map(e => {
                        return {
                            label: e.ConfigName,
                            value:e.Id
                        }
                    })
                })
                Request({
                    url: "/SystemConfig/GetDepartmentList",
                    method: 'post',
                }).then(res => {
                    params.deptList.value = res.Data.map(e => {
                        return {
                            label: e.DepartmentName,
                            value: e.Id,

                        }
                    })
                    params.sdeptList.value = res.Data.map(e => {
                        return {
                            label: e.DepartmentName,
                            value: e.Id,

                        }
                    })
                })





            })

         


    

        },
        handleDeptChange: (item) => {
    
            Request({
                url: "/SystemConfig/GetUserListByDepartment",
                method: 'post',
                data: {
                    DepartmentId: item.value
                }
            }).then(res => {

                params.formState.value.name=""
                params.formState.value.tel = ""
                params.memberList.value = res.Data.map(e => {
                    return {
                        label: `${e.UserName}-${e.UserNo}`,
                        value: `${e.Phone}-${e.UserNo}`,

                    }
                })
            })
        },
        handleUserChange: () => {


            params.formState.value.tel = params.formState.value.name.value && params.formState.value.name.value.split('-')[0]
        },
        delData: (id) => {


     
        },

        handleconfirmOk: (id) => {
            Request({
                url: "/Performance/DeleteTeamMember",
                method: 'post',
                data: {
                    Id:id
                }
            }).then(res => {

                params.messageTest.value.success(res.Msg);
                methods.getTableList()

            }).finally(() => {
    
            })

        },
        handleOk: () => {
            params.proxy.value.$refs.formRef.validate()
                .then(() => {
                 
                    if (params.opType.value == 0) {
                        let subform = {
                            UserNo: params.formState.value.name.label.split('-')[1],
                            JobTitle: params.formState.value.job,
                            JobRole: params.formState.value.role,
                        }
                        Request({
                            url: "/Performance/AddTeamMember",
                            method: 'post',
                            data: subform
                        }).then(res => {
                            params.messageTest.value.success(res.Msg);

                            params.proxy.value.$refs.formRef&&params.proxy.value.$refs.formRef.resetFields();

                            methods.getTableList()
                        }).finally(() => {
                            params.open.value = false
                        })
                    } else {

                        if (params.opType.value == 2) {
                            params.open.value = false
                            return
                        }

                        params.chooseMember.value.JobRole = params.formState.value.role
                        params.chooseMember.value.JobTitle = params.formState.value.job
                        params.chooseMember.value.UserNo = params.formState.value.name.value ? params.formState.value.name.label.split('-')[1] : params.chooseMember.value.UserNo
                        Request({
                            url: "/Performance/EditTeamMember",
                            method: 'post',
                            data: params.chooseMember.value
                        }).then(res => {

                            params.messageTest.value.success(res.Msg);
                            params.proxy.value.$refs.formRef&&params.proxy.value.$refs.formRef.resetFields();
                            methods.getTableList()
                        }).finally(() => {
                            params.open.value = false
                        })
                    }
                }).catch(error => {
                    if (error.errorFields.length != 0) {
                        params.messageTest.value.warn(error.errorFields[0].errors[0])
                    }
            
                });
        }
    }
    const template = {
        test: {
            template: `<div @click="add">+</div> <div>{{testnum}}</div>`,
          
            setup() {
     
                return {
                    ...params,
                    ...methods,
                };
            }

        }
    }
    if (proxy) {

        params.proxy.value = proxy
    }

    //let template = toRefs(vtemplate)
    return { // 返回对象给模板

        ...params,
        ...methods,
        template

    }
}


export default Composition